using NJG;
using UnityEngine;

[AddComponentMenu("NJG MiniMap/NGUI/World Map")]
[ExecuteInEditMode]
public class UIWorldMap : UIWorldMapBase
{
	private static UIWorldMap mInst;

	public GameObject marker;

	private GameObject _CurMarker;

	private UISlider slider;

	protected int createdFrameNumber;

	private NJGMapItem markerItem;

	public static UIWorldMap instance
	{
		get
		{
			if (mInst == null)
			{
				mInst = Object.FindObjectOfType(typeof(UIWorldMap)) as UIWorldMap;
			}
			return mInst;
		}
	}

	public void OnHoverOver()
	{
		isMouseOver = true;
	}

	public void OnHoverOut()
	{
		isMouseOver = false;
	}

	public void OnPress(bool isPress)
	{
		tapOnMap = isPress;
	}

	public void OnDoubleClick()
	{
	}

	protected override void OnStart()
	{
		if (NJGMap.instance == null)
		{
			return;
		}
		if (mInst == null)
		{
			mInst = this;
		}
		base.OnStart();
		if (!(planeRenderer == null))
		{
			BoxCollider component = planeRenderer.GetComponent<BoxCollider>();
			if (component == null)
			{
				NGUITools.AddWidgetCollider(planeRenderer.gameObject);
			}
			UIForwardEvents uIForwardEvents = planeRenderer.GetComponent<UIForwardEvents>();
			if (uIForwardEvents == null)
			{
				uIForwardEvents = planeRenderer.gameObject.AddComponent<UIForwardEvents>();
			}
			uIForwardEvents.onClick = true;
			uIForwardEvents.onHover = true;
			uIForwardEvents.onDoubleClick = true;
			uIForwardEvents.target = base.gameObject;
		}
	}

	protected override void Update()
	{
		base.Update();
		ZoomMap();
	}

	private void ZoomMap()
	{
		if (Input.touchCount != 2)
		{
			return;
		}
		Touch touch = Input.GetTouch(0);
		Touch touch2 = Input.GetTouch(1);
		Vector2 vector = touch.position - touch.deltaPosition;
		Vector2 vector2 = touch2.position - touch2.deltaPosition;
		float magnitude = (vector - vector2).magnitude;
		float magnitude2 = (touch.position - touch2.position).magnitude;
		float num = magnitude - magnitude2;
		if (Mathf.Abs(num) > 0.01f)
		{
			MapWindow.instance.setZoomWorldMap(MapWindow.instance.GetZoomWorldMap() - num * 0.002f);
			if (slider == null)
			{
				slider = base.gameObject.GetComponentInChildren<sliderWorldMapChangeVal>().gameObject.GetComponent<UISlider>();
			}
			slider.value = MapWindow.instance.GetZoomWorldMap();
		}
	}

	protected override UIMapIconBase GetEntry(NJGMapItem item)
	{
		UISpriteData uISpriteData = null;
		int i = 0;
		for (int count = mList.Count; i < count; i++)
		{
			UIMapIcon uIMapIcon = (UIMapIcon)mList[i];
			if (!(uIMapIcon == null) && uIMapIcon.item.Equals(item))
			{
				string text = NJGMap.instance.GetSprite(item.type).name;
				if (uIMapIcon.sprite.spriteName != text || uIMapIcon.typeIcon != item.type)
				{
					uIMapIcon.sprite.spriteName = text;
					uIMapIcon.sprite.depth = item.depth;
					uIMapIcon.sprite.color = NJGMapBase.instance.GetColor(item.type);
					uIMapIcon.sprite.width = (int)item.iconScale.x;
					uIMapIcon.sprite.height = (int)item.iconScale.y;
					uIMapIcon.typeIcon = item.type;
				}
				return uIMapIcon;
			}
		}
		if (mUnused.Count > 0)
		{
			UIMapIcon uIMapIcon2 = (UIMapIcon)mUnused[mUnused.Count - 1];
			uIMapIcon2.item = item;
			uIMapIcon2.typeIcon = item.type;
			uIMapIcon2.sprite.spriteName = NJGMap.instance.GetSprite(item.type).name;
			uIMapIcon2.sprite.depth = item.depth;
			uIMapIcon2.sprite.color = item.color;
			if (uIMapIcon2.sprite.localSize != (Vector2)item.iconScale)
			{
				if (uIMapIcon2.collider != null)
				{
					if (uIMapIcon2.collider is BoxCollider)
					{
						BoxCollider boxCollider = uIMapIcon2.collider as BoxCollider;
						boxCollider.size = item.iconScale;
					}
					if (uIMapIcon2.collider is BoxCollider2D)
					{
						BoxCollider2D boxCollider2D = uIMapIcon2.collider as BoxCollider2D;
						boxCollider2D.size = item.iconScale;
					}
				}
				uIMapIcon2.sprite.width = (int)item.iconScale.x;
				uIMapIcon2.sprite.height = (int)item.iconScale.y;
			}
			uISpriteData = NJGMap.instance.GetSpriteBorder(item.type);
			if (uISpriteData != null && uIMapIcon2.border != null)
			{
				uIMapIcon2.border.spriteName = uISpriteData.name;
				uIMapIcon2.border.depth = item.depth;
				uIMapIcon2.border.color = item.color;
				if (uIMapIcon2.border.localSize != (Vector2)item.borderScale)
				{
					uIMapIcon2.border.width = (int)item.borderScale.x;
					uIMapIcon2.border.height = (int)item.borderScale.y;
				}
			}
			mUnused.RemoveAt(mUnused.Count - 1);
			NGUITools.SetActive(uIMapIcon2.gameObject, true);
			mList.Add(uIMapIcon2);
			return uIMapIcon2;
		}
		if (Time.frameCount == createdFrameNumber)
		{
			return null;
		}
		createdFrameNumber = Time.frameCount;
		GameObject gameObject = NGUITools.AddChild(base.iconRoot.gameObject);
		gameObject.name = "Icon" + mCount;
		UISprite uISprite = NGUITools.AddWidget<UISprite>(gameObject);
		uISprite.name = "Icon";
		uISprite.depth = 1 + NGUITools.CalculateNextDepth(uISprite.gameObject) + item.depth;
		uISprite.atlas = NJGMap.instance.atlas;
		uISprite.spriteName = NJGMap.instance.GetSprite(item.type).name;
		uISprite.color = item.color;
		uISprite.width = (int)item.iconScale.x;
		uISprite.height = (int)item.iconScale.y;
		UIMapIcon uIMapIcon3 = gameObject.AddComponent<UIMapIcon>();
		uIMapIcon3.item = item;
		uIMapIcon3.sprite = uISprite;
		uIMapIcon3.typeIcon = item.type;
		if (item.interaction)
		{
			if (gameObject.GetComponent<Collider>() == null)
			{
				NGUITools.AddWidgetCollider(gameObject);
				uIMapIcon3.collider = gameObject.GetComponent<Collider>();
				if (uIMapIcon3.collider is BoxCollider)
				{
					BoxCollider boxCollider2 = uIMapIcon3.collider as BoxCollider;
					boxCollider2.size = item.iconScale;
				}
				if (uIMapIcon3.collider is BoxCollider2D)
				{
					BoxCollider2D boxCollider2D2 = uIMapIcon3.collider as BoxCollider2D;
					boxCollider2D2.size = item.iconScale;
				}
			}
			UISprite uISprite2 = null;
			uISpriteData = NJGMap.instance.GetSpriteBorder(item.type);
			if (uISpriteData != null)
			{
				uISprite2 = NGUITools.AddWidget<UISprite>(gameObject);
				uISprite2.name = "Selection";
				uISprite2.depth = 1 + NGUITools.CalculateNextDepth(uISprite2.gameObject) + item.depth + 1;
				uISprite2.atlas = NJGMap.instance.atlas;
				uISprite2.spriteName = uISpriteData.name;
				uISprite2.color = item.color;
				uISprite2.width = (int)item.borderScale.x;
				uISprite2.height = (int)item.borderScale.y;
				uIMapIcon3.border = uISprite2;
			}
		}
		if (uIMapIcon3 == null)
		{
			Debug.LogError("Expected to find a Game Map Icon on the prefab to work with", this);
			Object.Destroy(gameObject);
		}
		else
		{
			mCount++;
			uIMapIcon3.item = item;
			mList.Add(uIMapIcon3);
		}
		return uIMapIcon3;
	}

	public void OnClick()
	{
		Vector3 point = UICamera.lastHit.point;
		Vector3 vector = base.cachedTransform.InverseTransformPoint(point);
		vector -= base.cachedTransform.position;
		Vector3 position = MapToWorld(vector);
		bool flag = _CurMarker == null || !_CurMarker.activeSelf;
		if (_CurMarker == null)
		{
			_CurMarker = Object.Instantiate(marker, position, Quaternion.identity) as GameObject;
			_CurMarker.name = "Marker";
			markerItem = _CurMarker.GetComponent<NJGMapItem>();
			_CurMarker.transform.SetParent(base.gameObject.transform);
		}
		if (flag)
		{
			_CurMarker.transform.position = position;
			markerItem.AddToList();
		}
		_CurMarker.SetActive(flag);
		needUpdateIcons = true;
	}
}
